﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CatalogSupplierCommentTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("title", "text")]
        public void Can_CRUD(string title, string text)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                Flags = CatalogSupplierFlags.CommentAllowed
            }).Result;

            #endregion

            var comment = Processor.Execute(new CreateCatalogSupplierComment
            {
                AccountID = account.EntityID,
                HolderID = supplier.EntityID,
                Title = title,
                Text = text
            }).Result;

            Assert.NotNull(comment);
            comment.Title.AreEqualIgnoringCase(title);
            comment.Text.AreEqualIgnoringCase(text);
            comment.AreEqual(Processor.Execute(new GetCatalogSupplierComment
            {
                EntityID = comment.EntityID
            }).Result);

            title = title.Reverse();
            text = text.Reverse();
            var upd_comment = Processor.Execute(new UpdateCatalogSupplierComment
            {
                EntityID = comment.EntityID,
                Title = title,
                Text = text
            }).Result;

            Assert.NotNull(upd_comment);
            upd_comment.AreEqual(comment);
            upd_comment.Title.AreEqualIgnoringCase(title);
            upd_comment.Text.AreEqualIgnoringCase(text);

            Processor.Execute(new DeleteCatalogSupplierComment
            {
                EntityID = comment.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Text_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCatalogSupplierComment
            {
                Text = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Text_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateCatalogSupplierComment
            {
                Text = String.Empty
            }));
        }

        [Test]
        public void Cannot_Create_If_Not_Allowed()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCatalogSupplierComment
            {
                AccountID = 0,
                HolderID = Processor.Execute(new CreateCatalogSupplier
                {
                    Flags = CatalogSupplierFlags.None
                }).Result.EntityID,
                Title = "title",
                Text = "text"
            }));
        }
    }
}
